package binary_tree

//二叉树节点定义
type TreeNode struct {
	Val   int
	Left  *TreeNode
	Right *TreeNode
}

type BinaryTree struct {
	root *TreeNode
}

//初始化二叉树
//前序:(根-左-右) 1-2-4-5-3
//中序:(左-根-右) 4-2-5-1-3
//后序:(左-右-根) 4-5-2-3-1
//    1
//   / \
//  2   3
// / \
//4  5
func CreateBinaryTreeA() *BinaryTree {
	tree := &BinaryTree{}
	tree.root = &TreeNode{
		Val: 1,
		Left: &TreeNode{
			Val: 2,
			Left: &TreeNode{
				Val:   4,
				Left:  nil,
				Right: nil,
			},
			Right: &TreeNode{
				Val:   5,
				Left:  nil,
				Right: nil,
			},
		},
		Right: &TreeNode{
			Val:   3,
			Left:  nil,
			Right: nil,
		},
	}
	return tree
}

//使用数组创建 TODO
//0代表空节点
var listIndex = -1

func CreateBinaryTree(list []int) *TreeNode {
	listIndex++
	if listIndex >= len(list) {
		return nil
	}

	//0代表终止节点
	if list[listIndex] == 0 {
		return nil
	}
	node := &TreeNode{}
	node.Val = list[listIndex]
	node.Left = CreateBinaryTree(list)
	node.Right = CreateBinaryTree(list)
	return node
}
